import { defineConfig ,loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'

import { fileURLToPath, URL } from 'node:url'

import vueJsx from '@vitejs/plugin-vue-jsx';
import { join, resolve } from "path"

export default defineConfig(({ command, mode }) => {
  // 获取各种环境下对应的变量
  let env = loadEnv(mode, process.cwd())
  return {
    plugins: [
      vue(),
      vueJsx()
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    // 代理跨域
    server: {
      host: '0.0.0.0',
      proxy: {
        // API 接口代理
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(`^${env.VITE_APP_BASE_API}`), ''),
        },
        // WebSocket/SockJS 代理
        '/user/ws': {
          target: env.VITE_SERVE, // 使用同一个后端地址或单独配置
          changeOrigin: true,
          ws: true, // 启用 WebSocket 代理
          rewrite: (path) => path.replace(/^\/user\/ws/, '') // 可选的重写路径
        }
      },
    },
  }
})
